package com.example.SocialApplication.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.SocialApplication.R;
import com.example.SocialApplication.activity.lists.AddNewListActivity;
import com.example.SocialApplication.db.entity.User;

import java.util.List;

/**
 * Created by differ on 25.08.13.
 */
public class PeoplesAdapter extends ArrayAdapter {
    private List<User> list;
    private Context context;

    public PeoplesAdapter(Context context, int textViewResourceId, List<User> objects) {
        super(context, textViewResourceId, objects);
        this.list = objects;
        this.context = context;
    }

    @Override
    public User getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = ((Activity) context).getLayoutInflater().inflate(R.layout.people_grid_item, parent, false);
        }

        ImageView image = (ImageView) view.findViewById(R.id.people_grid_item_image);
        byte[] decodedString = Base64.decode(getItem(position).getPhoto(), Base64.DEFAULT);  // TODO crashing app if Base64 wrong
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        if(decodedByte != null) {
            image.setImageBitmap(decodedByte);
        } else {
            image.setImageResource(android.R.drawable.ic_menu_report_image);
        }
        return view;
    }
}
